package org.nstack.network.securitygroup;

import org.springframework.http.HttpMethod;
import org.nstack.header.configuration.PythonClassInventory;
import org.nstack.header.identity.Action;
import org.nstack.header.message.APIEvent;
import org.nstack.header.message.APIMessage;
import org.nstack.header.message.APIParam;
import org.nstack.header.notification.ApiNotification;
import org.nstack.header.rest.RestRequest;

import java.util.List;

import static java.util.Arrays.asList;


@Action(category = SecurityGroupConstant.ACTION_CATEGORY)
@RestRequest(
        path = "/security-groups/{securityGroupUuid}/rules",
        method = HttpMethod.POST,
        responseClass = APIAddSecurityGroupRuleEvent.class,
        parameterName = "params"
)
public class APIAddSecurityGroupRuleMsg extends APIMessage {

    @PythonClassInventory
    public static class SecurityGroupRuleAO {

        private String type;

        private Integer startPort;

        private Integer endPort;

        private String protocol;

        private String allowedCidr;

        public String getType() {
            return type;
        }

        public void setType(String type) {
            this.type = type;
        }

        public Integer getStartPort() {
            return startPort;
        }

        public void setStartPort(Integer startPort) {
            this.startPort = startPort;
        }

        public Integer getEndPort() {
            return endPort;
        }

        public void setEndPort(Integer endPort) {
            this.endPort = endPort;
        }

        public String getProtocol() {
            return protocol;
        }

        public void setProtocol(String protocol) {
            this.protocol = protocol;
        }

        public String getAllowedCidr() {
            return allowedCidr;
        }

        public void setAllowedCidr(String allowedCidr) {
            this.allowedCidr = allowedCidr;
        }
    }


    @APIParam(resourceType = SecurityGroupVO.class, checkAccount = true, operationTarget = true)
    private String securityGroupUuid;

    @APIParam(nonempty = true)
    private List<SecurityGroupRuleAO> rules;

    public String getSecurityGroupUuid() {
        return securityGroupUuid;
    }

    public List<SecurityGroupRuleAO> getRules() {
        return rules;
    }

    public void setRules(List<SecurityGroupRuleAO> rules) {
        this.rules = rules;
    }

    public void setSecurityGroupUuid(String securityGroupUuid) {
        this.securityGroupUuid = securityGroupUuid;
    }
    
 
    public static APIAddSecurityGroupRuleMsg __example__() {
        APIAddSecurityGroupRuleMsg msg = new APIAddSecurityGroupRuleMsg();
        msg.setSecurityGroupUuid(uuid());
        SecurityGroupRuleAO rule = new SecurityGroupRuleAO();
        rule.setType("Ingress");
        rule.setAllowedCidr("0.0.0.0/0");
        rule.setStartPort(22);
        rule.setEndPort(22);
        rule.setProtocol("TCP");
        msg.setRules(asList(rule));
        return msg;
    }

    public ApiNotification __notification__() {
        APIMessage that = this;

        return new ApiNotification() {
            @Override
            public void after(APIEvent evt) {
                if (evt.isSuccess()) {
                    ntfy("Added").resource(((APIAddSecurityGroupRuleEvent)evt).getInventory().getUuid(),SecurityGroupRuleVO.class.getSimpleName())
                            .messageAndEvent(that, evt).done();
                }
            }
        };
    }
}
